package com.nop.biz.demo2;

import io.nop.api.core.annotations.biz.BizModel;
import io.nop.api.core.annotations.biz.BizMutation;
import io.nop.api.core.annotations.biz.BizQuery;
import io.nop.api.core.annotations.core.Name;
import io.nop.api.core.annotations.graphql.GraphQLReturn;
import io.nop.api.core.beans.FilterBeans;
import io.nop.api.core.beans.query.QueryBean;
import io.nop.core.reflect.bean.BeanTool;
import io.nop.dao.api.IDaoProvider;
import io.nop.dao.api.IEntityDao;
import io.nop.orm.IOrmEntity;
import jakarta.inject.Inject;

import java.util.List;
import java.util.Map;

/**
 * <strong>   </strong> <br>
 * <p>
 * <strong> </strong> <br>
 * </p>
 * <br>
 *
 * @author :
 * @date 2024年01月31日
 * 修改人 修改日期 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@BizModel("Demo2Entity")
public class Demo2EntityBizModel {

    @Inject
    IDaoProvider daoProvider;


    @BizQuery
    @GraphQLReturn(bizObjName = "Demo2Entity")
    public IOrmEntity getEntity(@Name("id") String id) {
        IEntityDao<IOrmEntity> dao = daoProvider.dao("app.demo.Demo2Entity");
        return dao.getEntityById(id);
    }


    @BizMutation
    @GraphQLReturn(bizObjName = "Demo2Entity")
    public IOrmEntity saveEntity(@Name("data") Map<String, Object> data) {
        IEntityDao<IOrmEntity> dao = daoProvider.dao("app.demo.Demo2Entity");
        IOrmEntity entity = dao.newEntity();
        BeanTool.instance().setProperties(entity, data);
        dao.saveEntity(entity);
        return entity;
    }
    @BizQuery
    @GraphQLReturn(bizObjName = "Demo2Entity")
    public List<IOrmEntity> findByName(@Name("name") String name) {
        IEntityDao<IOrmEntity> dao = daoProvider.dao("app.demo.Demo2Entity");

        QueryBean query = new QueryBean();
        query.addFilter(FilterBeans.contains("name", name));
        // <contains name="name" value="a" />  name like '%a%'
        return dao.findAllByQuery(query);
    }


    // 注意，字段不能声明为private。NopIoC无法注入私有成员变量
    @Inject
    Demo2Mapper demo2Mapper;

    @BizQuery
    @GraphQLReturn(bizObjName = "Demo2Entity")
    public IOrmEntity findBySql(@Name("name") String name) {
        return demo2Mapper.findFirstByName(name);
    }


}
